package com.me.blood.frag;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.lang.reflect.Field;

public class BaseDialogFragment extends DialogFragment {

    private OnDismissListener mListener;
    private boolean isOk;
    private Object value;

    @Override
    @CallSuper
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, getDlgTheme());
    }

    protected int getDlgTheme() {
        return com.menu.myapplication.R.style.MyDialogTheme;
    }

    @Override
    public LayoutInflater onGetLayoutInflater(@Nullable Bundle savedInstanceState) {
        LayoutInflater inflater = super.onGetLayoutInflater(savedInstanceState);
        if (getDialog() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        return inflater;
    }

    @Override
    @CallSuper
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
        } catch (IllegalStateException e) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    @CallSuper
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mListener != null) {
            mListener.onDismiss(isOk, value);
        }
    }

    public void setOnDismissListener(OnDismissListener listener) {
        mListener = listener;
    }

    public interface OnDismissListener {
        void onDismiss(boolean isOk, Object value);
    }

    public void dismiss(boolean isOk, Object value) {
        this.isOk = isOk;
        this.value = value;
        dismiss();
    }

    @Override
    public void dismiss() {
        try {
            Field f = DialogFragment.class.getDeclaredField("mDismissed");
            f.setAccessible(true);
            f.set(this, false);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        try {
            dismissAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void replaceFragment(int id, Fragment fragment) {
        try {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.replace(id, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

//    protected void showLoading() {
//        try {
//            ((BaseActivity) getActivity()).showLoading(true);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//    }
//
//    protected void hideLoading() {
//        try {
//            ((BaseActivity) getActivity()).hideLoading();
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//    }
//
//    protected boolean isLoading() {
//        try {
//            return ((BaseActivity) getActivity()).isLoading();
//        } catch (Throwable e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
}
